package com.ruoyi.xsc.controller;

import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.annotation.InnerAuth;

import com.ruoyi.xsc.domain.Merchant;
import com.ruoyi.xsc.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商户Controller
 * 
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseController
{
    @Autowired
    private IMerchantService merchantService;

    /**
     * 查询商户列表
     */
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/list")
    public TableDataInfo list(Merchant merchant)
    {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);
//        SMSUtil.send("15837524281", "code:123456");
//        System.out.println("发送成功");
        return getDataTable(list);
    }

    @PostMapping("/merchantlist")
    @InnerAuth
    public TableDataInfo merchantlist(@RequestBody Merchant merchant)
    {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);

        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @RequiresPermissions("merchant:merchant:export")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant)
    {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商户数据");
    }

    /**
     * 获取商户详细信息
     */
    @RequiresPermissions("merchant:merchant:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(merchantService.selectMerchantById(id));
    }

    /**
     * 新增商户
     */
    @RequiresPermissions("merchant:merchant:add")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Merchant merchant)
    {
        System.out.println(merchant);
        merchant.setRegTime(new Date());
     merchantService.insertMerchant(merchant);
        return AjaxResult.success(merchant.getId());
    }
    @InnerAuth
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping("merchantAdd")
    public AjaxResult merchantAdd(@RequestBody Merchant merchant)
    {
        System.out.println(merchant);
        merchant.setRegTime(new Date());
        merchantService.insertMerchant(merchant);
        return AjaxResult.success(merchant.getId());
    }
    /**
     * 修改商户
     */
    @RequiresPermissions("merchant:merchant:edit")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant)
    {
        return toAjax(merchantService.updateMerchant(merchant));
    }


    /**
     * 回收商户
     */
//    @RequiresPermissions("merchant:merchant:edit")
//    @Log(title = "商户", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult recycle(@PathVariable Long ids)
//    {
//        Merchant merchant = new Merchant();
//        merchant.setId(ids);
//        long ss=0;
//        merchant.setDel(ss);
//        return toAjax(merchantService.updateMerchant(merchant));
//    }
/*商户排名*/
    @RequiresPermissions("merchant:merchant:list")
    @GetMapping("/merchantorderbyscore")
    public TableDataInfo merchantorderbyscore()
    {
        startPage();
        List<Map> maps = merchantService.selectMerchantOrderByscore();

        return getDataTable(maps);
    }

}
